﻿using AutoMapper;
using EFEntity;
using Iservices;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Services
{
    public class salary_grantServices : Isalary_grantServices
    {
        private readonly MyDbContext myDBContext;
        private readonly IMapper mapper;

        public salary_grantServices(MyDbContext myDBContext, IMapper mapper)
        {
            this.myDBContext = myDBContext;
            this.mapper = mapper;
        }

        public async Task<int> Add(salary_grant t)
        {
            Esalary_grant test = new Esalary_grant()
            {
                first_kind_id = t.first_kind_id,
                first_kind_name = t.first_kind_name,
                second_kind_id = t.second_kind_id,
                second_kind_name = t.second_kind_name,
                third_kind_id = t.third_kind_id,
                third_kind_name = t.third_kind_name,
                salary_grant_id = t.salary_grant_id,
                salary_paid_sum = t.salary_paid_sum,
                salary_standard_id = t.salary_standard_id,
                salary_standard_sum = t.salary_standard_sum,
                human_amount = t.human_amount,
                register = t.register,
                regist_time = t.regist_time,
                checker = t.checker,
                check_status = t.check_status,
                check_time = t.check_time
            };
            myDBContext.salary_grant.Add(test);
            return await myDBContext.SaveChangesAsync();
        }


        public async Task<int> Delete(int id)
        {
            Esalary_grant test = new Esalary_grant()
            {
                sgr_id = id
            };
            myDBContext.salary_grant.Attach(test);
            myDBContext.salary_grant.Remove(test);
            return await myDBContext.SaveChangesAsync();
        }

        public async Task<List<salary_grant>> FenYe(FenYe fy)
        {
            var data = myDBContext.salary_grant.OrderBy(e => e.sgr_id).ToList();
            fy.Rows = data.Count();
            fy.Pages = (fy.Rows - 1) / fy.PageSize + 1;
            List<Esalary_grant> list = new List<Esalary_grant>();
            await Task.Run(() =>
            {
                list = data.Skip((fy.CurrentPage - 1) * fy.PageSize)
                    .Take(fy.PageSize)
                    .ToList();
            });
            List<salary_grant> list2 = new List<salary_grant>();
            foreach (Esalary_grant t in list)
            {
                salary_grant ck = new salary_grant()
                {
                    sgr_id = t.sgr_id,
                    first_kind_id = t.first_kind_id,
                    first_kind_name = t.first_kind_name,
                    second_kind_id = t.second_kind_id,
                    second_kind_name = t.second_kind_name,
                    third_kind_id = t.third_kind_id,
                    third_kind_name = t.third_kind_name,
                    salary_grant_id = t.salary_grant_id,
                    salary_paid_sum = t.salary_paid_sum,
                    salary_standard_id = t.salary_standard_id,
                    salary_standard_sum = t.salary_standard_sum,
                    human_amount = t.human_amount,
                    register = t.register,
                    regist_time = t.regist_time,
                    checker = t.checker,
                    check_status = t.check_status,
                    check_time = t.check_time
                };
                list2.Add(ck);
            }
            return list2;
        }

        public async Task<List<salary_grant>> FenYeSel(FenYe fy)
        {
            SqlParameter[] ps = {
                new SqlParameter() {ParameterName="@pageSize",Value=fy.PageSize},
                new SqlParameter() {ParameterName="@keyName",Value="sgr_id" },
                new SqlParameter() {ParameterName="@tabelName",Value="salary_grant" },
                new SqlParameter() {ParameterName="@where",Value=fy.Where},
                new SqlParameter() {ParameterName="@currentPage",Value=fy.CurrentPage},
               // new SqlParameter() {ParameterName="@lie",Value="*" },
                new SqlParameter() {ParameterName="@rows",Direction= ParameterDirection.Output,SqlDbType= SqlDbType.Int },
                new SqlParameter() {ParameterName="@pages",Direction= ParameterDirection.Output,SqlDbType= SqlDbType.Int }
                };
            List<Esalary_grant> list = await myDBContext.salary_grant.FromSqlRaw("exec proc_FenYe @pageSize, @keyName, @tabelName, @where, @currentPage, @rows output, @pages output", ps).ToListAsync();
            fy.Rows = (int)ps[5].Value;
            fy.Pages = (int)ps[6].Value;
            List<salary_grant> list2 = new List<salary_grant>();

            foreach (Esalary_grant t in list)
            {
                salary_grant ck = new salary_grant()
                {
                    sgr_id = t.sgr_id,
                    first_kind_id = t.first_kind_id,
                    first_kind_name = t.first_kind_name,
                    second_kind_id = t.second_kind_id,
                    second_kind_name = t.second_kind_name,
                    third_kind_id = t.third_kind_id,
                    third_kind_name = t.third_kind_name,
                    salary_grant_id = t.salary_grant_id,
                    salary_paid_sum = t.salary_paid_sum,
                    salary_standard_id = t.salary_standard_id,
                    salary_standard_sum = t.salary_standard_sum,
                    human_amount = t.human_amount,
                    register = t.register,
                    regist_time = t.regist_time,
                    checker = t.checker,
                    check_status = t.check_status,
                    check_time = t.check_time
                };
                list2.Add(ck);
            }
            return list2;
        }

        public async Task<List<salary_grant>> SelectAll()
        {

            List<Esalary_grant> list = new List<Esalary_grant>();
            await Task.Run(() =>
            {
                list = myDBContext.salary_grant.ToList();
            });

            List<salary_grant> list2 = new List<salary_grant>();
            foreach (Esalary_grant t in list)
            {
                salary_grant ck = new salary_grant()
                {
                    sgr_id = t.sgr_id,
                    first_kind_id = t.first_kind_id,
                    first_kind_name = t.first_kind_name,
                    second_kind_id = t.second_kind_id,
                    second_kind_name = t.second_kind_name,
                    third_kind_id = t.third_kind_id,
                    third_kind_name = t.third_kind_name,
                    salary_grant_id = t.salary_grant_id,
                    salary_paid_sum = t.salary_paid_sum,
                    salary_standard_id = t.salary_standard_id,
                    salary_standard_sum = t.salary_standard_sum,
                    human_amount = t.human_amount,
                    register = t.register,
                    regist_time = t.regist_time,
                    checker = t.checker,
                    check_status = t.check_status,
                    check_time = t.check_time
                };
                list2.Add(ck);
            }
            return list2;
        }

        public Task<salary_grant> SelectID(int id)
        {
            throw new NotImplementedException();
        }

        public async Task<int> Update(salary_grant t)
        {
            Esalary_grant sa = new Esalary_grant()
            {
                sgr_id = t.sgr_id,
                first_kind_id = t.first_kind_id,
                first_kind_name = t.first_kind_name,
                second_kind_id = t.second_kind_id,
                second_kind_name = t.second_kind_name,
                third_kind_id = t.third_kind_id,
                third_kind_name = t.third_kind_name,
                salary_grant_id = t.salary_grant_id,
                salary_paid_sum = t.salary_paid_sum,
                salary_standard_id = t.salary_standard_id,
                salary_standard_sum = t.salary_standard_sum,
                human_amount = t.human_amount,
                register = t.register,
                regist_time = t.regist_time,
                checker = t.checker,
                check_status = t.check_status,
                check_time = t.check_time
            };
            myDBContext.salary_grant.Attach(sa);
            myDBContext.Entry(sa).State = EntityState.Modified;
            return await myDBContext.SaveChangesAsync();
        }


        public async Task<Dictionary<string, object>> FenYe_grant_Async(int currentpage,string where)
        {
            List<Esalary_grant> list = new List<Esalary_grant>();
            Dictionary<string, object> di = new Dictionary<string, object>();
            //调用分页存储过程
            SqlParameter[] ps = {
                new SqlParameter() {ParameterName="@pageSize",Value=3 },
                new SqlParameter() {ParameterName="@keyName",Value="res_id" },
                new SqlParameter() {ParameterName="@tabelName",Value="salary_grant" },
                new SqlParameter() {ParameterName="@where",Value=where},
                new SqlParameter() {ParameterName="@currentPage",Value=currentpage },
               // new SqlParameter() {ParameterName="@lie",Value="*" },
                new SqlParameter() {ParameterName="@rows",Direction= ParameterDirection.Output,SqlDbType= SqlDbType.Int },
                new SqlParameter() {ParameterName="@pages",Direction= ParameterDirection.Output,SqlDbType= SqlDbType.Int }
                };
            list = await myDBContext.salary_grant.FromSqlRaw
                ("exec proc_FenYe @pageSize, @keyName, @tabelName, @where, @currentPage, @rows output, @pages output", ps)
                     .ToListAsync();
            int rows = (int)ps[5].Value;
            int pages = (int)ps[6].Value;
            List<salary_grant> list2 = mapper.Map<List<salary_grant>>(list);

            di["list"] = list2;
            di["rows"] = rows;
            di["pages"] = pages;
            return di;
        }
    }
}
